﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SevenZip;
using System.IO;

namespace JTS_Archiver
{
    public partial class progresscompress : Form
    {
        public progresscompress(Form1 parent, string in_archFile, OutArchiveFormat in_archType, CompressionLevel in_compLevel, string[] in_fileList)
        {
            InitializeComponent();
            archFile = in_archFile;
            archType = in_archType;
            compLevel = in_compLevel;
            fileList = in_fileList;
            owner = parent;
        }

        public string archFile;
        public OutArchiveFormat archType;
        public CompressionLevel compLevel;
        public string[] fileList;
        public SevenZipCompressor enginec = new SevenZipCompressor();
        public int curFileNum = 0;
        public Form1 owner;

        private void progresscompress_Load(object sender, EventArgs e)
        {

        }

        private void progresscompress_Shown(object sender, EventArgs e)
        {
            startCompression();
        }

        public void comp_Compressing(object sender, ProgressEventArgs e)
        {
            progressBar1.Increment(e.PercentDelta);
            Refresh();
        }

        public void comp_FileCompressionStarted(object sender, FileNameEventArgs e)
        {
            label3.Text = Path.GetFileName(e.FileName);
            progressBar2.Increment(1);
            curFileNum++;
            label4.Text = curFileNum.ToString() + " / " + fileList.Length.ToString() + " files compressed";
        }

        public void comp_CompressionFinished(object sender, EventArgs e)
        {
            progressBar1.Value = progressBar1.Maximum;
            progressBar2.Value = progressBar2.Maximum;
            label4.Text = "Compression complete!";
            FileInfo fi = new FileInfo(archFile);
            string fz = owner.fSize((ulong)fi.Length);
            MessageBox.Show("Compression has been completed successfully."+Environment.NewLine+Environment.NewLine+"Output file is "+fz, "Compression Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        public void startCompression()
        {
            enginec.Compressing += new EventHandler<ProgressEventArgs>(comp_Compressing);
            enginec.FileCompressionStarted += new EventHandler<FileNameEventArgs>(comp_FileCompressionStarted);
            enginec.CompressionFinished += new EventHandler<EventArgs>(comp_CompressionFinished);
            enginec.ArchiveFormat = archType;
            enginec.CompressionLevel = compLevel;
            label2.Text = Path.GetFileName(archFile);
            if (File.Exists(archFile))
            {
                File.Delete(archFile);
            }
            progressBar2.Maximum = fileList.Length;
            label4.Text = curFileNum.ToString() + " / " + fileList.Length.ToString() + " files compressed";
            enginec.BeginCompressFiles(archFile, fileList);
        }
    }
}
